package com.wzcl.app.dao.repository;


import com.wzcl.app.dao.model.Commodity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CommodityRepository extends JpaRepository<Commodity,Integer>, JpaSpecificationExecutor<Commodity> {

    @Modifying
    @Query("UPDATE Commodity m SET m.state = 1 WHERE m.resourceId = ?1 AND m.type = ?2 ")
    void deleteByResourceIdAndType(Integer resourceId, Integer resourceType);

    /**
     * 根据id查询商品是否下架
     * @param id 商品id
     * @return 返回商品上架状态
     */
    @Query(value = "select c.sale from Commodity c where c.id = ?1")
    Boolean findSaleById(Integer id);

    /**
     * 通过商品id查询课程id
     * @param id 商品id
     * @return
     */
    @Query(value = "select c.resourceId from Commodity c where c.id = ?1")
    Integer findResourceIdById(Integer id);

    /**
     * 通过商品id查询商品
     * @param id 商品id
     * @return
     */
    Commodity findAllById(Integer id);
}
